// 基础信息部分接口
var express = require('express');
var router = express.Router();
const {
    sqlConnect
} = require('../sql/sqlUtil');
const Return = require("../util/utils");
// 添加支付信息
router.post("/add", function (req, res, next) {
    const charge_unit = req.body.charge_unit,
        card_id = req.body.card_id,
        bankname = req.body.bankname,
        src = req.body.src;
    let sql="INSERT INTO system_data (charge_unit,card_id,bankname,qr_url,is_del,del) VALUES(?,?,?,?,0,0)"
    sqlConnect(sql,[charge_unit,card_id,bankname,src],function(err,data){
        if (err) {
            console.log(err);
        }else {
            var json=Return.setResult();
            json.count=data.length;
            json.data = data;
            res.send(json);
        }
    })
})
// 查询表信息
router.get("/", function (req, res, next) {
    sqlConnect("select * from system_data where del=0", function (err, data) {
        if (err) {
            console.log(err);
        } else {
            var json = Return.setResult();
            json.code = 0;
            json.count = data.length;
            json.data = data;
            res.send(json);
        }
    })
})
// 返回编辑行信息
router.post("/", function (req, res, next) {
    var id = req.body.id;
    sqlConnect("select * from system_data where id=" + id, function (err, data) {
        if (err) {
            console.log(err);
        } else {
            var json = Return.setResult();
            json.count = data.length;
            json.data = data;
            res.send(json);
        }
    })
})
// 修改基础信息
router.put("/", function (req, res) {
    var charge_unit = req.body.charge_unit;
    var card_id = req.body.card_id;
    var bankname = req.body.bankname;
    var src = req.body.src;
    var id = req.body.sid;
    var sql = "update system_data set charge_unit=?,card_id=?,bankname=?,qr_url=? where id=?";
    var sqlArr = [charge_unit, card_id, bankname, src, id];
    sqlConnect(sql, sqlArr, function (err, data) {
        data = Return.setResult();
        if (err) {
            data.code = 500;
            data.message = "服务器内部异常!"
            res.send(data);
        } else {
            data.message = "修改成功"
            res.send(data);
        }
    })
})
// 启用停用基础信息
router.put("/switch", (req, res) => {
    let id = req.body.id,
        is_del = req.body.is_del;
    let sql=null,
    sqlArr=null;
    if(is_del===0){
        sql="update system_data set is_del=?";
        sqlArr=[0];
    }else {
        sql="update system_data set is_del=? where id=?;update system_data set is_del=0 where id!=?";
        sqlArr=[1,id,id];
    }
    sqlConnect(sql,sqlArr, (err) => {
        if (err) {
            return console.log(err);
        }
        res.send(Return.setResult());
    })
})
// 删除基础信息
router.delete("/", function (req, res, next) {
    try {
        const id = req.body.id;
    let sql = `update system_data set del=1,is_del=0 where id in(${id})`;
    sqlConnect(sql, id, (err, data) => {
        if (err) {
            res.send(Return.setResult());
        } else {
            var json = Return.setResult();
            json.msg = "删除成功";
            res.send(json);
        }
    })
    } catch (error) {
        console.log(error);
    }
    
})
module.exports = router;